﻿using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThongTinNhanKhau.GUI
{
    public partial class NhanKhau : Form
    {
        model.BO.NhanKhauBO nhanKhauBO;
        public NhanKhau()
        {
            InitializeComponent();
        }

        private void NhanKhau_Load(object sender, EventArgs e)
        {
            nhanKhauBO = new model.BO.NhanKhauBO(this);
            loadTable();

            repositoryItemCheckEdit2.ValueChecked = "Y";
            repositoryItemCheckEdit2.ValueUnchecked = "N";

            panelTimKiem.Visible = true;
            panelUpdate.Visible = false;
            this.gridcontrolNhanKhau.Location = new System.Drawing.Point(1, 131);
            this.gridcontrolNhanKhau.Size = new System.Drawing.Size(1004, 528);
        }

        public void loadTable()
        {
            nhanKhauBO.loadTable();
        }



        private void btnXoa_Click(object sender, EventArgs e)
        {
            txtMaNhanKhau.Text = "";
            txtCMND.Text = "";
            txtHoTen.Text = "";
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            nhanKhauBO.timKiem();
        }

        private void gridViewNhanKhau_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            GridView view = sender as GridView;

            GridHitInfo hitInfo = view.CalcHitInfo(e.Point);

            if (hitInfo.InRow)
            {
                view.FocusedRowHandle = hitInfo.RowHandle;
                this.contextMenuStripNhanKhau.Show(view.GridControl, e.Point);
            }
            else
            {
                this.contextMenuStripNhanKhau.Show(view.GridControl, e.Point);
            }
        }

        private void toolStripMenuItemThemMoi_Click(object sender, EventArgs e)
        {
            this.gridcontrolNhanKhau.Location = new System.Drawing.Point(1, 219);
            this.gridcontrolNhanKhau.Size = new System.Drawing.Size(1004, 440);
            this.panelTimKiem.Visible = false;
            this.panelUpdate.Visible = true;

            nhanKhauBO.generationID();
            this.cboUGioiTinh.SelectedIndex = 0;
            this.txtUHoTen.Text = "";
            this.txtUTrinhDo.Text = "";
            this.txtUQueQuan.Text = "";
            this.txtUCMND.Text = "";
            this.txtUNTT.Text = "";
            this.dateTimeNgaySinh.Text = DateTime.Now.ToShortDateString();

        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.gridcontrolNhanKhau.Location = new System.Drawing.Point(1, 131);
            this.gridcontrolNhanKhau.Size = new System.Drawing.Size(1004, 528);
            this.panelTimKiem.Visible = true;
            this.panelUpdate.Visible = false;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            nhanKhauBO.btnLuu();
        }

        private void ToolStripMenuItemUpdate_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                this.gridcontrolNhanKhau.Location = new System.Drawing.Point(1, 219);
                this.gridcontrolNhanKhau.Size = new System.Drawing.Size(1004, 440);
                this.panelTimKiem.Visible = false;
                this.panelUpdate.Visible = true;

                this.txtUMNK.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                this.txtUHoTen.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "HoTen").ToString();
                this.txtUTrinhDo.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "TrinhDo").ToString();
                this.txtUQueQuan.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "QueQuan").ToString();
                this.txtUCMND.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "CMND").ToString();
                this.txtUNTT.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "NoiThuongTru").ToString();
                this.dateTimeNgaySinh.Text = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "NgaySinh").ToString();

                //gioitinh
                if ("Nam".Equals(gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "GioiTinh").ToString()))
                {
                    this.cboUGioiTinh.SelectedIndex = 0;
                }
                else
                {
                    this.cboUGioiTinh.SelectedIndex = 1;
                }
            }
        }

        private void xóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String tenNhanKhau = this.gridViewNhanKhau.GetRowCellValue(this.gridViewNhanKhau.FocusedRowHandle, "HoTen").ToString();
                DialogResult result = MessageBox.Show("Bạn có muốn xóa Nhân Khẩu: " + tenNhanKhau + "???", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                //If Yes
                if (result == DialogResult.Yes)
                {
                    String maNhanKhau = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                    nhanKhauBO.deleteNhanKhau(maNhanKhau);
                }
               
            }
        }

        private void ToolStripMenuItemQLTamTru_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maNhanKhau = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                GUI.TamTru tamTru = new GUI.TamTru(this,maNhanKhau);
                tamTru.Show();
            }
            
        }

        private void quảnLýTạmVắngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maNhanKhau = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                GUI.TamVang tamVang = new TamVang(this, maNhanKhau);
                tamVang.Show();
            }
        }

        private void quảnLýTATSToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maNhanKhau = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                GUI.TienAnTienSu tienAnTienSu = new TienAnTienSu(this, maNhanKhau);
                tienAnTienSu.Show();
            }
        }

        private void quảnLýChứngTửToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.gridViewNhanKhau.SelectedRowsCount > 0)
            {
                String maNhanKhau = gridViewNhanKhau.GetRowCellValue(gridViewNhanKhau.FocusedRowHandle, "MaNhanKhau").ToString();
                GUI.ChungTu chungTu = new ChungTu(this, maNhanKhau);
                chungTu.Show();
            }
        }
    }
}
